/* Copyright (C) 2013 Dr2co
 *
 * Created with IntelliJ IDEA.
 * User: Dr2co
 * Date: 20.07.13
 *
 *  pt-emu is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  pt-emu is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with pt-emu.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.aionengine.gameserver.utils.api.vk;

import com.aionengine.gameserver.configs.main.JsonConfig;
import com.aionengine.gameserver.utils.api.ReadUrl;
import javolution.util.FastList;
import org.json.JSONArray;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author Dr2co
 */


public class JsonVkLoader {

    public static class VkItem {

        private int id;
        private long date;
        private String text;
        private int comments;
        private int like;
        private String groupName;
        private String name;

        public VkItem(int id, long date, String text, int comments, int like, String groupName, String name) {
            this.id = id;
            this.date = date;
            this.text = text;
            this.comments = comments;
            this.like = like;
            this.groupName = groupName;
            this.name = name;
        }

        public int getId() {
            return id;
        }

        public long getDate() {
            return date;
        }

        public String getText() {
            return text;
        }

        public int getCommnets() {
            return comments;
        }

        public int getLikes() {
            return like;
        }

        public String getGroupName() {
            return groupName;
        }

        public String getName() {
            return name;
        }

        public String getTimeFormat() {
            Date dt = new Date(getDate() * 1000L);
            SimpleDateFormat format = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss");
            return format.format(dt);
        }
    }

    private static FastList<VkItem> itemList;

    public static FastList<VkItem> load(int groupId, int count) {
        itemList = FastList.newInstance();
        try {
            JSONObject js = new JSONObject(ReadUrl.read("http://api.vk.com/method/wall.get?owner_id=-" + groupId + "&filter=owner&count=" + count + ""));
            JSONArray response = js.getJSONArray("response");
            JSONObject js2 = new JSONObject(ReadUrl.read("http://api.vk.com/method/groups.getById?gid=" + groupId));
            JSONArray response2 = js2.getJSONArray("response");
            for (int i = 1; i < response.length(); i++) {
                JSONObject jo = response.getJSONObject(i);
                String text = "";
                if (jo.getString("text").length() > 0) {
                    try {
                        text = jo.getString("text").substring(0, JsonConfig.VK_TEXT_LENGTH) + "...";
                    } catch (Exception ex) {
                        text = jo.getString("text");
                    }
                }

                VkItem item = new VkItem(jo.getInt("id"), jo.getLong("date"), text, jo.getJSONObject("comments").getInt("count"), jo.getJSONObject("likes").getInt("count"), response2.getJSONObject(0).getString("screen_name"), response2.getJSONObject(0).getString("name"));
                itemList.add(item);
            }
        } catch (Exception ex) {
            System.out.println(ex);
        }
        return itemList;
    }
}